package com.mhkj.base.itemInfo.building.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.itemInfo.building.entity.Building;
import com.mhkj.base.itemInfo.building.service.BuildingService;
import com.mhkj.base.itemInfo.phase.entity.Phase;
import com.mhkj.base.itemInfo.phase.service.PhaseService;
import com.mhkj.base.region.service.NationService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 楼栋
 */

@Controller
@RequestMapping("/Building")
public class BuildingController extends BaseController<Building> {
	@Autowired
	private BuildingService buildingService;
	@Autowired
	private NationService nationService;
	@Autowired
	private PhaseService phaseService;
	@Autowired
	private OrgService orgService;
	
	/**
	 * 分页查询楼栋
	 * 
	 */
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<Building> listByPage( Building Building, int pageIndex, int pageSize ) {
		Pagination<Building> pagination = buildingService.listByPage(Building,pageIndex, pageSize);
		return pagination;
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(Building Building) {
		
		Long nature = Building.getArchitecturalNature();
		if(StringUtils.isEmpty(nature)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "建筑性质为空!");
		}
	 
		Long phaseId = Building.getPhaseId();
		if(StringUtils.isEmpty(phaseId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "期区为空!");
		}
		 Phase phase = phaseService.getById(phaseId);
		Building.setPhase(phase);
		String message = buildingService.save(Building);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(Building Building) {
		Long nature = Building.getArchitecturalNature();
		if(StringUtils.isEmpty(nature)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "建筑性质为空!");
		}
	 
		Long phaseId = Building.getPhaseId();
		if(StringUtils.isEmpty(phaseId)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "期区为空!");
		}
		 Phase phase = phaseService.getById(phaseId);
		Building.setPhase(phase);
		String message = buildingService.save(Building);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del",method={RequestMethod.POST})
	@ResponseBody
	public ResponseMessage del(Long id) {
		if(StringUtils.isEmpty(id)) {
			return ResponseMessage.createMessage(false, ErrorCode.PARAM_ERROR, "id为空!");
		}
		Building Building = new Building();
		Building.setId(id);
		String message = buildingService.del(Building);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

 
	/**
	 * 查询楼栋信息
	 */
	@RequestMapping(value="/info", method=RequestMethod.POST)
	@ResponseBody
	public Building info(@RequestParam(value = "id", required = true) long id) {
		Building Building = buildingService.getById(id);
		return Building;
	}


	/**
	 * 批量删除楼栋
	 */
	@RequestMapping(value="/delAll", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delAll(@RequestParam(value="id",required=true) String id) {
		String[] split = id.split(",");
		for (String str : split) {
			long parseLong = Long.parseLong(str);
			Building Building = buildingService.getById(parseLong);
			buildingService.del(Building);
		}
		
		return ResponseMessage.createMessage(true, "ok");
	}
 
 
	}
